import { ref } from "vue";
import { getParkInfoApi } from '../../api/park'
import * as echarts from 'echarts'
import { Application } from '@splinetool/runtime';
// 园区相关信息---------------------
export function useGetParkInfoApi() {
  const parkInfo = ref({})
  const getParkInfo = async () => {
    const result = await getParkInfoApi()
    console.log(result);
    if (result.code !== 10000) return
    parkInfo.value = result.data
  }
  return {
    parkInfo,  // 数据
    getParkInfo // 函数
  }
}
// 园区年度分析(柱状图)--------------
export function useInitBarChartApi(parkInfo) {
  const barChart = ref(null)
  const initBarChart = () => {
    const { parkIncome } = parkInfo.value
    // 1. 初始化echarts实例
    // 2. 准备配置项 option
    // 3. 将实例和配置项关联 
    const myChart = echarts.init(barChart.value);
    const option = {
      tooltip: {
        trigger: 'axis',
        axisPointer: {
          type: 'shadow',
        },
      },
      grid: {
        // 让图表占满容器
        top: '10px',
        left: '0px',
        right: '0px',
        bottom: '0px',
        containLabel: true,
      },
      xAxis: [
        {
          type: 'category',
          axisTick: {
            alignWithLabel: true,
            show: false,
          },
          data: parkIncome.xMonth,
        },
      ],
      yAxis: [
        {
          type: 'value',
          splitLine: {
            show: false,
          },
        },
      ],
      series: [
        {
          name: '园区年度收入',
          type: 'bar',
          barWidth: '10px',
          data: parkIncome.yIncome.map((item, index) => {
            const color =
              index % 2 === 0
                ? new echarts.graphic.LinearGradient(0, 0, 0, 1, [
                  { offset: 0.23, color: '#74c0f8' },
                  { offset: 1, color: 'rgba(116,192,248,0.00)' },
                ])
                : new echarts.graphic.LinearGradient(0, 0, 0, 1, [
                  { offset: 0.23, color: '#ff7152' },
                  { offset: 1, color: 'rgba(255,113,82,0.00)' },
                ])
            return { value: item, itemStyle: { color } }
          }),
        },
      ],
      textStyle: {
        color: '#B4C0CC',
      },
    }
    myChart.setOption(option);
  }
  return {
    barChart,
    initBarChart
  }
}
// 园区产业分布(饼图)-----------------
export function useInitPieChartApi(parkInfo) {
  const pieChart = ref(null)
  const initPieChart = () => {
    const { parkIndustry } = parkInfo.value
    const myChart = echarts.init(pieChart.value);
    const option = {
      color: [
        '#00B2FF', '#2CF2FF', '#892CFF', '#FF624D', '#FFCF54', '#86ECA2'],
      legend: {
        itemGap: 20,
        bottom: '0',
        icon: 'rect',
        itemHeight: 10, // 图例icon高度
        itemWidth: 10, // 图例icon宽度
        textStyle: {
          color: '#c6d1db',
        },
      },
      tooltip: {
        trigger: 'item'
      },
      series: [
        {
          name: '园区产业分析',
          type: 'pie',
          radius: ['55%', '60%'], // 设置内圈与外圈的半径使其呈现为环形
          center: ['50%', '40%'], // 圆心位置， 用于调整整个图的位置
          tooltip: {
            trigger: 'item',
            formatter: (params) => {
              return `${params.seriesName}</br><div style='display:flex;justify-content: space-between;'><div>${params.marker}${params.name}</div><div>${params.percent}%</div></div>`
            }
          },
          label: {
            show: false,
            position: 'center',
          },
          data: parkIndustry,
        },
      ],

    }
    myChart.setOption(option)
  }
  return {
    pieChart,
    initPieChart
  }
}
// 渲染3d模型------------------------
export function useInit3dModelApi(loading) {
  const ref3d = ref(null)
  const init3dModel = async () => {
    // 初始化3d模型
    // 初始化前 - loading -》 true
    loading.value = true
    // 初始化后 - loading -》 false
    const spline = new Application(ref3d.value);
    // load 解析(加载) + 渲染
    // load('xxxx') 地址指的就是3d模型
    await spline.load('https://prod.spline.design/6Wq1Q7YGyM-iab9i/scene.splinecode');
    // await spline.load('https://fe-hmzs.itheima.net/scene.splinecode');
    // 模型渲染完成了
    loading.value = false
  }
  return {
    ref3d,
    init3dModel
  }
}